package com.local.admin.controller;

import com.local.common.util.ResultUtil;
import com.local.coupon.feign.CouponServiceFeign;
import com.local.coupon.pojo.Coupon;
import com.local.coupon.pojo.CouponListRequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Controller
@RequestMapping("/admin/coupon")
public class AdminCouponController {

    @Autowired
    private CouponServiceFeign couponServiceFeign;

    @GetMapping
    public String index(HttpServletRequest request) {
        request.setAttribute("path", "coupon");
        return "admin/coupon";
    }


    /**
     * 查询数据列表，支持翻页和搜索
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param couponName
     * @param couponType 优惠券赠送类型，如果是0则通用券，用户领取；如果是1，则是注册赠券；如果是2，则是优惠券码兑换；
     * @param couponStatus 优惠券状态，如果是0则是正常可用；如果是1则是过期; 如果是2则是下架。
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @RequestParam("sidx") String sort, @RequestParam("order") String order,
                           @RequestParam(name = "couponName", required = false) String couponName,
                           @RequestParam(name = "couponType", required = false) Integer couponType,
                           @RequestParam(name = "couponStatus", required = false) Integer couponStatus,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime) {
        page = Objects.isNull(page) || page <= 0 ? 1 : page;
        limit = Objects.isNull(limit) || limit <= 0 ? 1 : limit;
        sort = Objects.isNull(sort) || sort.isEmpty() ? "coupon_id" : sort;
        order = Objects.isNull(order) || order.isEmpty() ? "desc" : order;

        CouponListRequestParam requestParam = new CouponListRequestParam(page, limit, sort, order, couponName, couponType, couponStatus, startTime, endTime, -1);
        ResultUtil result = couponServiceFeign.couponList(requestParam);
        return result;
    }


    /**
     * 新增
     * @param coupon
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Coupon coupon) {
        coupon.setIsDeleted(0);
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        coupon.setCreateTime(dateTime);
        coupon.setUpdateTime(dateTime);
        ResultUtil saveResult = couponServiceFeign.couponSave(coupon);
        return saveResult;
    }


    /**
     * 通过id查询数据
     * @param couponId
     * @return
     */
    @GetMapping("/{couponId}")
    @ResponseBody
    public ResultUtil<Coupon> getById(@PathVariable("couponId") Integer couponId) {
        ResultUtil<Coupon> couponResult = couponServiceFeign.getCouponById(couponId);
        return couponResult;
    }


    /**
     * 修改
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Coupon coupon) {
        String dateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        coupon.setUpdateTime(dateTime);
        ResultUtil updateResult = couponServiceFeign.couponUpdate(coupon);
        return updateResult;
    }


    /**
     * 根据id逻辑删除
     * @param couponId
     * @return
     */
    @DeleteMapping("/{couponId}")
    @ResponseBody
    public ResultUtil delete(@PathVariable("couponId") Integer couponId) {
        ResultUtil deleteResult = couponServiceFeign.couponDelete(couponId);
        return deleteResult;
    }
}
